home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / Examples / NX_Invaders / NXIGameView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.0 KB  |  144 lines

  1.  
  2. #import "NXIGameView.h"
  3. #import "NXIGameBrain.h"
  4. #import <libc.h>    // event stuff, misc.
  5. #import <daymisckit/daymisckit.h>
  6. #import "NXIBullet.h"
  7. #import "NXISaucer.h"
  8. #import "NXIAlien.h"
  9. #import "NXIAlienOverseer.h"
  10. #import "NXIPlayer.h"
  11. //#import "NXIPlayerBullet.h"
  12.  
  13. // comment out the next line to disable the cheat modes.  Cheaters can't
  14. // get net high scores, but they can get local high scores.
  15. #define CHEATMODES YES
  16.  
  17. @implementation NXIGameView
  18.  
  19. - initFrame:(const NXRect *)frm    // designated initializer for a view
  20. {    
  21.     [super initFrame:frm];
  22.     return self;
  23. }
  24.  
  25. // Called by appDidInit to load up images, etc. that we need.
  26. - loadPix
  27. {
  28.     [super loadPix];    
  29.     return self;
  30. }
  31.  
  32. - autoUpdate:sender
  33. {            // ALL animation is controlled from here!!!
  34.     BOOL updateFlag = NO;
  35.  
  36.     // keep track of how many time we've been called; we can use it do
  37.     // decide when to do certain things...
  38.     if ([NXApp isHidden]) return self; // don't suck cycles if we're hidden
  39.     cycles++;
  40.     
  41.     if (state == GAMEOVER) {
  42.     // when demowait counter hits WAITFORDEMO, we start up demo mode.
  43.     // it basically restarts the game--but with "demoMode" turned on.
  44.         if (demoWait++ == WAITFORDEMO) {
  45.             state = NORMALSTATE;
  46.             [self restartGameForDemo:YES];
  47.             [controller unpause];
  48.             [[self window] setTitle:"NXInvaders Demo"]; // ***** localize!
  49.             [scoreKeeper resetScore];
  50.     }    }
  51.     if (state == NORMALSTATE) {
  52.     }
  53.     // draw all the changes, if applicable.
  54.     if (updateFlag) [self update];
  55.     [self updateSelf:&bounds :1];
  56.     return self;
  57. }
  58.  
  59. - drawSelf:(const NXRect *)rects :(int)rectCount    // redraws the screen.
  60. {
  61.     return self;
  62. }
  63.  
  64. - updateSelf:(NXRect *)rects :(int)rectCount    // redraws the screen.
  65. {
  66.     return self;
  67. }
  68.  
  69. // Handle player movement.  We respond to the arrow keys.  If we don't
  70. // recognize the key, we pass it on.  This method just shunts the key
  71. // off to the player object, which is what really deals with it. 
  72. - keyDown:(NXEvent *)myevent
  73. {
  74.     unsigned short charCode;
  75.     unsigned short charSet;
  76.     
  77.     if (!myevent) return self; // if no event when coalescing, go away
  78.     PSobscurecursor();
  79.     // check for arrow keys or space bar
  80.     if (!(myevent->flags&(NX_CONTROLMASK|NX_ALTERNATEMASK|NX_COMMANDMASK))) {
  81.         charCode = myevent->data.key.charCode;
  82.         charSet = myevent->data.key.charSet;
  83.         
  84.         if (charSet == NX_SYMBOLSET) { // symbol set contains the arrows
  85.             if          (charCode == 0xAD) { // Up Arrow
  86.                 [player setDirection:GK_UP_DIRECTION];
  87.                 if (paused) [controller unpause];
  88.                 return self;
  89.             } else if (charCode == 0xAF) { // Down Arrow
  90.                 [player setDirection:GK_DOWN_DIRECTION];
  91.                 if (paused) [controller unpause];
  92.                 return self;
  93.             } else if (charCode == 0xAC) { // Left Arrow
  94.                 [player setDirection:GK_LEFT_DIRECTION];
  95.                 if (paused) [controller unpause];
  96.                 return self;
  97.             } else if (charCode == 0xAE) { // Right Arrow
  98.                 [player setDirection:GK_RIGHT_DIRECTION];
  99.                 if (paused) [controller unpause];
  100.                 return self;
  101.             }
  102.         } else if (myevent->data.key.charCode == ' ') { // Space Bar
  103.             [player setDirection:GK_NO_DIRECTION];
  104.             if (paused) [controller unpause];
  105.             return self;
  106.         } else if ((myevent->data.key.charCode == 'a') && cheatMode) {
  107.         } else {
  108.             [super keyDown:myevent];
  109.         }
  110.     } else [super keyDown:myevent];
  111.     [self keyDown:[NXApp peekAndGetNextEvent:NX_KEYDOWN]]; // coalesce keydowns
  112.     // this is done recursively...primitive, but easy to implement :-)
  113.     return self;
  114. }
  115.  
  116. - setUpScreen
  117. {        
  118.     [super setUpScreen];
  119.     return self;
  120. }
  121.  
  122. - restartGame
  123. {
  124.     return [self restartGameForDemo:NO];
  125. }
  126.  
  127. - restartGameForDemo:(BOOL)doingDemo
  128. {
  129.     if (demoMode) {
  130.         demoMode = NO;
  131.         [[self window] setTitle:"NXInvaders"]; // ***** localize
  132.     }
  133.     [scoreKeeper resetScore];    // clear the score
  134.     [scoreKeeper resetBonus:(-1)];    // clear all bonuses
  135.     [self getPreferences];    // make sure we're up to date
  136.     [player newPlayer];        // get a new base to play with
  137.     if (doingDemo) demoMode = YES;
  138.     [self update]; [self updateSelf:&bounds :1]; NXPing(); // show screen
  139.     // start up the sound player object with the "start game" music
  140.     return self;
  141. }
  142.  
  143. @end
  144.